package com.cooper.controllers.seller;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.cooper.constants.ResourceConstants;
import com.cooper.controllers.Controller;
import com.cooper.core.JsonResult;
import com.cooper.entity.DesignerEntity;
import com.cooper.entity.LikeDetailEntity;
import com.cooper.entity.ProjectEntity;
import com.cooper.entity.UserInfo;
import com.cooper.pagination.PageInfo;
import com.cooper.service.admin.AddressService;
import com.cooper.service.item.ItemInfoService;
import com.cooper.service.item.LikeDetailService;
import com.cooper.service.project.ProjectService;
import com.cooper.service.seller.DesignerService;
import com.cooper.util.Controllers;
import com.cooper.vo.AddressVo;
import com.cooper.vo.ItemInfoVo;
import com.cooper.vo.ItemSearchVo;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.portal.Portal;

public class SellerHomeController extends Controller{

	@Autowired 
	DesignerService designerService;
	
	@Autowired
	ProjectService projectService;
	
	@Autowired
	AddressService addressService;
	
	@Autowired
	ItemInfoService itemInfoService;
	
	@Autowired
	LikeDetailService likeDetailService;
	
	@Get
	public String execute(Invocation inv,Portal portal,@Param("sellerId") Long sellerId) throws Exception{
		inv.addModel("sellerId", sellerId);
		UserInfo userInfo = Controllers.getUserInfo(inv);
		DesignerEntity designer = designerService.findDesignerByUserId(sellerId);
		AddressVo addressVo = this.addressService.getAddressVoByOriginAndOuterId(ResourceConstants.AddressOrigin.SELLER_ADDRESS, sellerId);
		inv.addModel("designer", designer);
		inv.addModel("addressVo", addressVo);
		inv.addModel("itemCount", this.itemInfoService.queryItemInfoCount(sellerId, new ItemSearchVo()));
		LikeDetailEntity like = null;
		if(userInfo!=null){
		  like = likeDetailService.getEntityByUserId(ResourceConstants.LikeOrigin.SHOP_LIKE, sellerId, userInfo.getId());
		}
		if(designer!=null&&StringUtils.isNotEmpty(designer.getLiveFileIds())){
			List<String> personLifeFiles =  Arrays.asList(designer.getLiveFileIds().split(","));
			inv.addModel("personLifeFiles", personLifeFiles);
		}
		if(like!=null){
			inv.addModel("like", like.getScore());
		}else{
			inv.addModel("like", "-1");
		}
		return "SellerHome";
	}
	
	@Get("projectInfoWindow")
	public Object projectInfoWindow(Invocation inv,@Param("sellerId") Long sellerId){
		StringBuffer buf = new StringBuffer();
		try{
			ProjectEntity project = projectService.findProjectByUserId(sellerId);
			buf.append("<a href='javascript:void(0)' onclick=\"$('#projectwarning').show()\"><img class='salerProjectImage' src='"+(project==null?"static/images/global/blank.png":("download/"+project.getFileId()))+"'/></a>");
			buf.append("<div class='salerProjectDescription'>"+(project==null?"":project.getDescription())+"</div>");
		}catch(Exception e){
			logger.error("SellerHomeController-designerInfoWindow-error",e);
		}
		return "@"+buf.toString();
	}
	
	@Get("itemInfoList")
	public Object itemInfoList(Invocation inv, @Param("sellerId") Long sellerId, PageInfo pageInfo){
		JsonResult json = new JsonResult();
		StringBuffer buf = new StringBuffer();
		try{
			pageInfo.setPageSize(9);
			pageInfo.setRowCount(itemInfoService.queryItemInfoCount(sellerId, new ItemSearchVo()));
            if(pageInfo.getPageIndex() <= pageInfo.getTotalPage()){
            	List<ItemInfoVo> itemList = itemInfoService.queryItemInfoList(sellerId, new ItemSearchVo(), pageInfo);
            	if(CollectionUtils.isNotEmpty(itemList)){
        		  for(int index=0; index < itemList.size(); index++){
        			  ItemInfoVo itemVo = itemList.get(index);
        			  buf.append("<div class='img"+(index+1)+" cell'><div class='wate_main'>");
        			  buf.append("<a href='item/detail?id="+itemVo.getId()+"' target='_blank'><img src="+itemVo.getMainUrl()+"/></a>");
        			  buf.append("<div class='wate_title'>");
        			  buf.append("<h2>"+itemVo.getTitle())
        			  	.append("&nbsp;").append(StringUtils.isNotEmpty(itemVo.getCnName())?itemVo.getCnName():"")
        			  	.append("</h2>");
        			  buf.append("<div class='wate_mong'><i>￥</i>"+itemVo.getPrice()+"</div>");
        			  buf.append("<div class='wate_buy'><a href='item/detail?id="+itemVo.getId()+"' target='_blank'>BUY NOW 立即购买</a></div>");
        			  buf.append("</div><div style='clear:both;'></div></div></div>");
        		  }
        		  
        		  json.setSuccess(true);
            	}
			}
            json.put("curPage", pageInfo.getPageIndex()+1);
            json.setMessage(buf.toString());
		}catch(Exception e){
			logger.error("SellerHomeController-itemInfoList-error", e);
			e.printStackTrace();
		}
		return json;
	}
		
}
